﻿using DevExpress.Web.ASPxTreeView;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Web.UI.WebControls;
using EntryExit.Utils;
using System.Web.UI;

namespace EntryExit
{
    public partial class WhereIsHe : BasePage
    {
        public Employee SelectedEmployee { get; set; }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (this.Master is MainMaster)
            {
                ((MainMaster)this.Master).MasterTreeViewNodeSelected += WhereIsHe_MasterTreeViewNodeSelected;
            }
        }

        void WhereIsHe_MasterTreeViewNodeSelected(object source, TreeViewNodeEventArgs e)
        {
            SelectedEmployee = EmployeeManagerContext.GetEmployee(SelectedEmployeeLPN);
            DisplaySelectedEmployeeMapDetails(SelectedEmployee);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    SelectedEmployee = EmployeeManagerContext.GetEmployee(SelectedEmployeeLPN);
                    DisplaySelectedEmployeeMapDetails(SelectedEmployee);
                }
            }
            catch (Exception ex)
            {
                //Console.WriteLine(ex.Message);
                Logger.Error("Page : WhereIsHe.aspx.cs, Event : Page_Load", ex);
                ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('An internal error occured. Reload the tool and try again. If issue still persists, please report to your administrator..!');", true);
            }
        }
        private void DisplaySelectedEmployeeMapDetails(Employee employee)
        {
            mapHimPanel.HeaderText = "Where is " + employee.EmployeeName + "?";

            DateTime startDate = DateTime.Now.Date.AddDays(-1).AddHours(00).AddMinutes(00).AddSeconds(00);//..ToShortDateString() + " 00:00:00";
            DateTime endDate = DateTime.Now.Date.AddHours(23).AddMinutes(59).AddSeconds(59);

            LocationStatus locStatus = EmployeeManagerContext.GetLatestLocationStatus(startDate, endDate, employee.CardHolderId);
            string locationMessage = string.Empty;
            if (locStatus != null)
            {
                locationStatusImage.Visible = true;
                if(locStatus.Action =="ENTRY")
                {
                    string statusImageUrl="~/Content/Images/";
                    if (locStatus.ActionGate.Contains("ATHULYA"))
                    {
                        statusImageUrl = statusImageUrl + "EmpInAthulya.png";
                        locationMessage = "ATHULYA";
                    }
                    else if (locStatus.ActionGate.Contains("LEELA") || locStatus.ActionGate.Contains("Leela"))
                    {
                        statusImageUrl = statusImageUrl + "EmpInLeela.png";
                        locationMessage = "LEELA";
                    }
                    else if (locStatus.ActionGate.Contains("SECTION A "))
                    {
                        statusImageUrl = statusImageUrl + "EmpInThejaswini.png";
                        locationMessage = "THEJASWINI";
                    }
                    else if (locStatus.ActionGate.Contains("SECTION B "))
                    {
                        statusImageUrl = statusImageUrl + "EmpInThejaswini.png";
                        locationMessage = "THEJASWINI";
                    }
                    else if (locStatus.ActionGate.Contains("SECTION C "))
                    {
                        statusImageUrl = statusImageUrl + "EmpInThejaswini.png";
                        locationMessage = "THEJASWINI";
                    }
                    else if (locStatus.ActionGate.Contains("SECTION D "))
                    {
                        statusImageUrl = statusImageUrl + "EmpInThejaswini.png";
                        locationMessage = "THEJASWINI";
                    }
                    else if (locStatus.ActionGate.Contains("KINFRA"))
                    {
                        statusImageUrl = statusImageUrl + "EmpInKinfra.png";
                        locationMessage = "KINFRA";
                    }
                    else
                    {
                        statusImageUrl = statusImageUrl + "EmpOut.png";
                        locationMessage = "EY campus";
                    }
                    locationStatusLabel.Text = string.Format("{0} is currently inside EY premises. Entered {1} on : {2} at {3}",
                        employee.EmployeeName, locationMessage, locStatus.LastStatusDateTime.ToShortDateString(), locStatus.LastStatusDateTime.ToShortTimeString());
                    locationStatusImage.ImageUrl = statusImageUrl;
                }
                else
                {
                    locationStatusLabel.Text = string.Format("{0} is currently outside EY premises. Left EY campus on : {1} at {2}",
                        employee.EmployeeName,locStatus.LastStatusDateTime.ToShortDateString(),locStatus.LastStatusDateTime.ToShortTimeString()) ;
                    locationStatusImage.ImageUrl = "~/Content/Images/EmpOut.png";
                }
            }
            else
            {
                locationStatusLabel.Text  = "No data available for today";
                locationStatusImage.Visible = false;
            }
        }
    }
}